#creo mis vistas
from django.shortcuts import render_to_response, get_object_or_404, HttpResponse
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from models import Serie, Capitulo
from django.template import RequestContext
from django.utils.translation import get_language
from django.conf import settings
from django import forms
import random

RESULTS_PER_PAGE = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 10)

def index(request):
        name="name_%s" % str(get_language())
        series_list = Serie.objects.order_by(name)
        aleatorio=random.randint(1, series_list.count())
        recomendado = Serie.objects.get(id=aleatorio)
        # si el valor de page no es int, devuelvo a la primera pagina
        paginator = Paginator(series_list, RESULTS_PER_PAGE)
        template="base_listado_series.html"
        try:
                page = request.GET.get('page','1')
        except ValueError:
                page = 1
        context = {
                'page': page,
                'paginator': paginator,
                'series_list': series_list,
                'recomendado': recomendado,
        }
        return render_to_response(template, context, context_instance=RequestContext(request))

def detail(request,serie_slug):
        name="name_%s" % str(get_language())
        s = get_object_or_404(Serie, slug=serie_slug)
        capitulos_list=s.capitulos.order_by(name)
        paginator = Paginator(capitulos_list, RESULTS_PER_PAGE)
        template="base_detalle_serie.html"
        try:
                page = request.GET.get('page','1')
        except ValueError:
                page = 1        
                
        context = {
                'page': page,
                'paginator': paginator,
                'capitulos': capitulos_list,
                'serie': s,
        }
        return render_to_response(template, context, context_instance=RequestContext(request))

def detail_episode(request, serie_slug, episode_id):
        episode = get_object_or_404(Capitulo, pk=episode_id)
        print(serie_slug)
        s = get_object_or_404(Serie, slug=serie_slug)
        template="base_detalle_capitulo.html"
        context = {
                'episode': episode,
                'serie': s,
        }
        return render_to_response(template, context, context_instance=RequestContext(request))